//
//  ConversationCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/20.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "ConversationCell.h"
#import <LKDBHelper/LKDBHelper.h>
#import "NSDate+Debug.h"
#import <WZLBadgeImport.h>

@interface ConversationCell ()
@property (nonatomic, strong) UIView *iconView;
@end

@implementation ConversationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    
    self.iconImgView = UIImageView.new;
    self.titleLabel = [UILabel k_labelWithFontSize:15 textColor:UIColor.k_t2Color];
    self.lastMsgLabel = [UILabel k_labelWithFontSize:12 textColor:UIColor.k_t3Color];
    self.timeLabel = [UILabel k_labelWithFontSize:10 textColor:UIColor.k_t4Color];
    
    
    
    [self.contentView addSubview:self.pContentView];
    [self.pContentView addSubview:self.iconImgView];
    [self.pContentView addSubview:self.titleLabel];
    [self.pContentView addSubview:self.lastMsgLabel];
    [self.pContentView addSubview:self.timeLabel];
    self.iconView = [UIView new];
    self.iconView.backgroundColor = UIColor.clearColor;
    [self.pContentView addSubview:self.iconView];

    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.bottom.mas_offset(-5);
    }];
    self.iconImgView.layer.cornerRadius = 25;
    self.iconImgView.layer.masksToBounds = YES;
    self.iconView.size = CGSizeMake(50, 50);
    self.iconView.badgeCenterOffset = CGPointMake(-5, 5);
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.iconImgView);
        make.size.equalTo(self.iconImgView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImgView.mas_right).offset(5);
        make.top.equalTo(self.iconImgView);
        make.right.equalTo(self.timeLabel.mas_left).offset(-5);
    }];
    
    [self.lastMsgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.right.mas_offset(-15);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(self.titleLabel);
    }];
    
    
    self.iconImgView.image = UIImage.k_imgName(@"placeholder_user");
    self.titleLabel.text = @"张三";
    self.lastMsgLabel.text = @"在吗，明天去哪里啊~";
    self.timeLabel.text = @"16:30";
    
        self.lastMsgLabel.highlightedTextColor =
        self.timeLabel.highlightedTextColor =
        self.titleLabel.highlightedTextColor = UIColor.k_t1Color;

    
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
    self.selectedBackgroundView.backgroundColor = UIColor.whiteColor;
    self.backgroundColor =
    self.contentView.backgroundColor = global_viewBackgroundColor;
    
}
//
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    [super setHighlighted:highlighted animated:animated];
    self.timeLabel.highlighted =
    self.lastMsgLabel.highlighted =
    self.titleLabel.highlighted = highlighted;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    self.timeLabel.highlighted =
//    self.lastMsgLabel.highlighted =
//    self.titleLabel.highlighted = selected;

//    if (selected == YES) {
//        self.contentView.backgroundColor = UIColor.whiteColor;
//    } else {
//        self.contentView.backgroundColor = global_viewBackgroundColor;
//    }

//    if (selected == YES) {
//        self.backgroundColor =
//        self.contentView.backgroundColor = UIColor.whiteColor;
//    } else {
//        self.backgroundColor =
//        self.contentView.backgroundColor = global_viewBackgroundColor;
//    }
//
    [super setSelected:selected animated:animated];
    
    
    
    NSLog(@"\n%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
}

- (void)setSession:(SessionItem *)session {
    _session = session;
    
    MsgItem *lastMsg = [MsgItem searchWithWhere:@{@"message_id": @(session.last_msg_id)}].firstObject;
    self.lastMsgLabel.text = [lastMsg lastMsgTip];
    self.timeLabel.text = [NSDate yyyy_MM_ddOrMM_ddWithTimestamp:lastMsg.createtime];
    
    [self.iconImgView sd_setImageWithURL:session.groupIcon.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
    self.titleLabel.text = session.groupName;
    
    if (session.unread_msg_count > 0) {
        GroupItem *group = [GroupItem searchSingleWithWhere:@{@"group_id": session.group_id} orderBy:nil];
        if (group.no_disturb == YES) {
            self.iconView.badge.frame = CGRectMake(self.iconView.width-8, 0, 8, 8);
            [self.iconView showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
        } else {
            [self.iconView showBadgeWithStyle:WBadgeStyleNumber value:session.unread_msg_count animationType:WBadgeAnimTypeNone];
        }
    } else {
        [self.iconView clearBadge];
    }
}

@end
